package com.jdzy.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdzy.model.common.vos.ResultA;
import com.jdzy.model.student.dtos.AppointmentsDto;
import com.jdzy.model.student.pojos.Appointments;
import com.jdzy.model.student.vos.AppointmentVo;
import com.jdzy.model.teacher.dtos.AppointmentsTDto;
import com.jdzy.student.mapper.AppointmentsMapper;
import com.jdzy.student.service.AppointmentsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class AppointmentsServiceImpl extends ServiceImpl<AppointmentsMapper, Appointments> implements AppointmentsService {
    private final AppointmentsMapper appointmentsMapper;

    /**
     * 学生一对一教师预约功能
     * @param appointmentsDto
     * @return
     */
    @Override
    public ResultA<String> makeAppointment(AppointmentsDto appointmentsDto) {
        Appointments appointments = new Appointments();
        BeanUtils.copyProperties(appointmentsDto, appointments);
        appointments.setStatus((byte) 0); // 初始状态为待确认
        save(appointments);
        return ResultA.ok("预约成功");
    }

    /**
     * 教师查询预约功能
     * @param teacherUsername
     * @return
     */
    @Override
    public ResultA<List<AppointmentVo>> getTeacherAppointments(String teacherUsername) {
        LambdaQueryWrapper<Appointments> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Appointments::getTeacherUsername, teacherUsername);
        List<Appointments> appointmentsList = list(queryWrapper);

        List<AppointmentVo> appointmentVoList = new ArrayList<>();
        for (Appointments appointments : appointmentsList) {
            AppointmentVo appointmentVo = new AppointmentVo();
            BeanUtils.copyProperties(appointments, appointmentVo);
            appointmentVoList.add(appointmentVo);
        }
        return ResultA.ok(appointmentVoList);
    }

    /**
     * 教师端 教师修改预约信息的状态
     * @param appointmentsTDto
     * @return
     */
    @Override
    public ResultA<String> updateStatus(AppointmentsTDto appointmentsTDto) {

        int rowsAffected = appointmentsMapper.updateAppointmentStatus(appointmentsTDto.getId(), appointmentsTDto.getStatus());

        if (rowsAffected > 0) {
            return ResultA.ok();
        } else {
            return ResultA.fail(202, "更新失败");
        }

    }
}
